package com.yk.govpro.service.noticeList.impl;

import com.yk.govpro.dao.common.api.ICommonDAO;
import com.yk.govpro.model.NoticelistEntity;
import com.yk.govpro.service.build.api.IURLBuildService;
import com.yk.govpro.service.noticeList.api.INoticeListCrawlerService;
import com.yk.govpro.service.request.api.IJSONRequestService;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zsyoung on 2017/5/19.
 */
@Service("noticeListCrawlerService")
public class NoticeListCrawlerServiceImpl implements INoticeListCrawlerService {
    @Autowired
    private ICommonDAO commonDAO;
    @Autowired
    private IJSONRequestService jsonRequestService;
    @Autowired
    private IURLBuildService urlBuildService;

    private  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public List<String> getDetailIDList(String url) {
        List<String> idList= new ArrayList<>();
        JSONObject jsonObject = this.jsonRequestService.requestJSONObject(this.urlBuildService.buildListIDURL(url));
        JSONArray jsonArray = jsonObject.getJSONArray("notices");
        for (int i=0;i<jsonArray.length();i++){
            idList.add(String.valueOf(jsonArray.getJSONObject(i).getString("id")));
        }
        return idList;
    }


    @Override
    public List<JSONObject> getNoticeList(String url) {
        List<JSONObject> jsonObjects=new ArrayList<>();
        jsonObjects.add(this.jsonRequestService.requestJSONObject(this.urlBuildService.buildListIDURL(url)));
        return jsonObjects;
    }

    @Override
    public List<NoticelistEntity> transform(JSONObject jsonObject) throws ParseException {
        List<NoticelistEntity> entities = new ArrayList<>();
        JSONArray jsonArray = jsonObject.getJSONArray("notices");
        for (int i = 0; i < jsonArray.length(); i++) {
            NoticelistEntity noticelistEntity = new NoticelistEntity();
            JSONObject noticeList = jsonArray.getJSONObject(i);

            noticelistEntity.setAgentName(noticeList.has("agentName")?noticeList.getString("agentName"):null);
            noticelistEntity.setBidBeginTime(noticeList.has("bigBeginTime")?new Timestamp(sdf.parse(noticeList.getString("issueTime")).getTime()):null);
            noticelistEntity.setBidEndTime(noticeList.has("bidEndTime")?new Timestamp(sdf.parse(noticeList.getString("bidEndTime")).getTime()):null);
            noticelistEntity.setBuyerName(noticeList.getString("buyerName"));
            noticelistEntity.setDistrictName(noticeList.getString("districtName"));
            noticelistEntity.setIsIssue(noticeList.getInt("isIssue"));
            noticelistEntity.setIssueTime(noticeList.has("issueTime")?new Timestamp(sdf.parse(noticeList.getString("issueTime")).getTime()):null);
            noticelistEntity.setLastModify(noticeList.has("lastModify")?noticeList.getString("lastModify"):null);
            noticelistEntity.setNoticeId(noticeList.getString("id"));
            noticelistEntity.setNoticeType(noticeList.getInt("noticeType"));
            noticelistEntity.setOpenBidTime(noticeList.has("openBidTime")?new Timestamp(sdf.parse(noticeList.getString("issueTime")).getTime()):null);
            noticelistEntity.setPackageNum(noticeList.has("packageNum")?noticeList.getInt("packageNum"):null);
            noticelistEntity.setProjectBudget(noticeList.has("projectBudget")?noticeList.getDouble("projectBudget"):null);
            noticelistEntity.setProjectDirectoryCode(noticeList.has("projectDirectoryCode")?noticeList.getString("projectDirectoryCode"):null);
            noticelistEntity.setProjectDirectoryName(noticeList.has("projectDirectoryName")?noticeList.getString("projectDirectoryName"):null);
            noticelistEntity.setProjectPurchaseWay(noticeList.has("projectPurchaseWay")?noticeList.getInt("projectPurchaseWay"):null);
            noticelistEntity.setState(noticeList.getInt("state"));
            noticelistEntity.setZbje(noticeList.getDouble("zbje"));
            noticelistEntity.setTitle(noticeList.getString("title"));
            noticelistEntity.setCreatorOrgName(noticeList.getString("creatorOrgName"));
            entities.add(noticelistEntity);
        }
        return entities;
    }

    @Override
    public List<NoticelistEntity> transform(List<JSONObject> jsonObjects) throws ParseException {
        List<NoticelistEntity> entities =new ArrayList<>();
        for (JSONObject jsonObject:jsonObjects){
            entities.addAll(this.transform(jsonObject));
        }
        return entities;
    }

    @Override
    public void saveNoticeList(NoticelistEntity noticelistEntity) {
                this.commonDAO.save(noticelistEntity);
    }

    @Override
    public void saveNoticeList(List noticelistEntities) {
                this.commonDAO.saveObjects(noticelistEntities);
    }




}
